package com.turing.post.transaction.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.MerchantBusinessDaySettlement;
import com.turing.post.transaction.util.MchBsTypeTradDetail;
import com.turing.post.transaction.util.MchBsTypeTradDetailDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "transaction.impl.MchBsTypeTradDetailDaoImpl")
@Remote
@Local
public class MchBsTypeTradDetailDaoImpl implements MchBsTypeTradDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 插入
	 */
	public void insert(MchBsTypeTradDetail mbttd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mbttd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MchBsTypeTradDetail> getAllMchBsTypeTradDetailByTran(
			String merchantNo, Date tranDate, String tranSeqNo) {
		final String sql = "select t.* from MchBsTypeTradDetail t where t.merchantNo = ?1 and t.tranDate = ?2 and t.tranSeqNo = ?3 ";
		Query query = _em.createNativeQuery(sql, MchBsTypeTradDetail.class);
		query.setParameter(1, merchantNo);
		query.setParameter(2, tranDate);
		query.setParameter(3, tranSeqNo);
		return query.getResultList();
	}

	/**
	 * 根据日期获取有交易的商户并且与所有激活的商户合并后去掉重复的。
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getAllMerchantBusinessMerchantNo(String issueCompanyNo,
			Date tranDate) {
		String sql = "select distinct(merchantno) from "
				+ "("
				+ "select merchantno from MchBsTypeTradDetail where trandate=?2 and merchantno like '"
				+ issueCompanyNo
				+ "%' "
				+ "union "
				+ "select merchantno from merchant where activate='1' and issueCompanyNo=?1)";
		Query query = _em.createNativeQuery(sql, String.class);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		List<String> list = query.getResultList();
		return list;

	}

	/**
	 * 根据发卡商号、某日、商户号按商户号及业务类型汇总消费交易日结数据
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param merchantNo
	 *            商户号(可选)
	 * @param tranDate
	 *            帐务日期
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MerchantBusinessDaySettlement> getAllMerchantBusinessDaySettlementByMerchantNo(
			String issueCompanyNo, String merchantNo, Date tranDate) {
		String sql = "select t.merchantNo, t.businessType, "
				+ "sum(t.CardAmount), sum(t.ScoreAmount), sum(t.OtherAmount)"
				+ "from MchBsTypeTradDetail t, Merchant m "
				+ "where m.issueCompanyNo = ?1 and t.merchantNo = m.merchantNo and t.tranDate = ?3 "
				+ "and ( COALESCE(LENGTH(TRIM( ?2 )),0) = 0 or t.merchantNo = ?2 ) "
				+ "and ( COALESCE(LENGTH(TRIM( t.businessType )),0) > 0 ) "
				+ "group by t.merchantNo, t.businessType";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		if (PCPSUtil.isEmpty(merchantNo))
			query.setParameter(2, null);
		else
			query.setParameter(2, merchantNo);
		query.setParameter(3, tranDate);
		List<Object[]> obj_list = query.getResultList();
		if (obj_list == null)
			return null;

		// 类型转换
		List<MerchantBusinessDaySettlement> mds_list = new ArrayList<MerchantBusinessDaySettlement>();
		for (Object[] objarr : obj_list) {
			MerchantBusinessDaySettlement mds = new MerchantBusinessDaySettlement();
			mds.setIssueCompanyNo(issueCompanyNo);
			mds.setMerchantNo(objarr[0].toString());
			mds.setTranDate(tranDate);
			mds.setBusinessType(objarr[1].toString());
			BigInteger cardamt = new BigInteger(objarr[2].toString());
			BigInteger scoreamt = new BigInteger(objarr[3].toString());
			mds.setConsume(cardamt.add(scoreamt));
			mds.setCancelPaid(BigInteger.ZERO);
			mds.setOtherPaid(new BigInteger(objarr[4].toString()));
			mds.setCost(BigInteger.ZERO);
			mds.setIncome(BigInteger.ZERO);
			mds.setSettRatio(BigInteger.ZERO);
			mds_list.add(mds);
		}

		return mds_list;
	}
}